#include "timercountwindow.h"
#include "tlabel.h"

#include <DTitlebar>

#include <QVBoxLayout>
#include <QDoubleSpinBox>
#include <QLabel>
#include <QToolButton>
#include <QLCDNumber>
#include <QtAlgorithms>
#include <QMediaPlayer>
#include <QTimer>
#include <QScreen>

TimerCountWindow::TimerCountWindow(QWidget *parent)
    : DBlurEffectWidget(parent)
    , m_secCount(0)
{
    setWindowFlags(windowType() | Qt::WindowType::WindowStaysOnTopHint);
    setFocus(Qt::FocusReason::MouseFocusReason);
    setFocus();

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(5, 5, 5, 5);

    DTitlebar *titlebar = new DTitlebar(this);
    //    titlebar->setFixedHeight(25);
    titlebar->setBackgroundTransparent(true);
    mainLayout->addWidget(titlebar, 0, Qt::AlignTop | Qt::AlignRight);

    // 参数输入布局
    QWidget *countInputWidget = new QWidget(this);
    countInputWidget->setMinimumHeight(100);
    mainLayout->addWidget(countInputWidget, 1, Qt::AlignTop);

    QHBoxLayout *countInputLayout = new QHBoxLayout;
    countInputLayout->setContentsMargins(0, 0, 0, 0);
    countInputWidget->setLayout(countInputLayout);

    // 输入计时框
    QDoubleSpinBox *inputBox = new QDoubleSpinBox(this);
    countInputLayout->addWidget(inputBox);

    // 小时单位
    QLabel *hourUnitLable = new QLabel(this);
    hourUnitLable->setText("小时");
    countInputLayout->addWidget(hourUnitLable);

    QToolButton *startBtn = new QToolButton(this);
    startBtn->setIcon(QIcon(":/icons/play.svg"));
    countInputLayout->addWidget(startBtn);

    // 计时布局
    QWidget *countdownWidget = new QWidget(this);
    countdownWidget->setMinimumHeight(100);
    mainLayout->addWidget(countdownWidget, 1, Qt::AlignTop);

    QHBoxLayout *countdownLayout = new QHBoxLayout;
    countdownLayout->setContentsMargins(0, 0, 0, 0);
    countdownWidget->setLayout(countdownLayout);

    QLCDNumber *countdownNum = new QLCDNumber(this);
    countdownNum->setDigitCount(8);
    countdownNum->display("12:34:50");
    countdownLayout->addWidget(countdownNum);

    QToolButton *stopBtn = new QToolButton(this);
    stopBtn->setIcon(QIcon::fromTheme(":/icons/stop.svg"));
    countdownLayout->addWidget(stopBtn);

    // 自定义标签测试
    TLabel *testLabel = new TLabel(this);
    testLabel->setText("自定义标签测试");
    mainLayout->addWidget(testLabel);

    // timer
    QTimer *timer = new QTimer(this);
    timer->setInterval(1000);
    m_secSum = 0;
    m_timeoutSec = 0;
    m_secCount = 0;
    m_minuteCount = 0;
    m_hourCount = 0;

    // player
    QMediaPlayer *player = new QMediaPlayer(this);
    player->setAudioRole(QAudio::Role::MusicRole);

    QString musicPath = QString("%1/%2").arg(qApp->applicationDirPath()).arg("xiaomi_remix.mp3");
    QMediaContent miMediaContent = QMediaContent(QUrl::fromLocalFile(musicPath));
    musicPath = QString("%1/%2").arg(qApp->applicationDirPath()).arg("note_nel.mp3");
    QMediaContent noteMediaContent = QMediaContent(QUrl::fromLocalFile(musicPath));

    player->setMedia(miMediaContent);

    // connection
    connect(startBtn, &QToolButton::clicked, this, [=] {
        // 获取计时秒数
        m_timeoutSec = qRound(inputBox->value() * 3600);
        if (0 == m_timeoutSec) {
            return;
        }

        countInputWidget->hide();
        countdownWidget->show();

        m_secSum = 0;
        m_hourCount = 0;
        m_minuteCount = 0;
        m_secCount = 0;
        QString dis = QString("%1:%2:%3")
                          .arg(m_hourCount, 2, 10, QChar('0'))
                          .arg(m_minuteCount, 2, 10, QChar('0'))
                          .arg(m_secCount, 2, 10, QChar('0'));
        countdownNum->display(dis);
        timer->start();
    });

    connect(stopBtn, &QToolButton::clicked, this, [=] {
        countInputWidget->show();
        countdownWidget->hide();

        timer->stop();
        m_secSum = 0;
        m_timeoutSec = 0;
        m_hourCount = 0;
        m_minuteCount = 0;
        m_secCount = 0;
    });

    connect(timer, &QTimer::timeout, this, [=] {
        m_secCount++;
        m_secSum++;

        if (60 == m_secCount) {
            m_secCount = 0;
            m_minuteCount++;
        }
        if (60 == m_minuteCount) {
            m_minuteCount = 0;
            m_hourCount++;
        }
        if (24 == m_hourCount) {
            m_hourCount = 0;
        }

        // 判断是否到时
        if (m_timeoutSec < m_secSum) {
            Q_EMIT stopBtn->clicked();

            int rand = qrand();
            qInfo() << "随机数" << rand;
            if (1 == (rand % 2)) {
                player->setMedia(miMediaContent);
            } else {
                player->setMedia(noteMediaContent);
            }
            player->play();
        }

        QString dis = QString("%1:%2:%3")
                          .arg(m_hourCount, 2, 10, QChar('0'))
                          .arg(m_minuteCount, 2, 10, QChar('0'))
                          .arg(m_secCount, 2, 10, QChar('0'));
        countdownNum->display(dis);

        testLabel->setText(QString("自定义标签测试，当前累计秒数：%1").arg(m_secSum));
    });

    // post init
    countInputWidget->show();
    countdownWidget->hide();

    QTimer::singleShot(10, this, [this] {
        QScreen *screen = QGuiApplication::primaryScreen();
        QRect screenRect = screen->availableVirtualGeometry();
        int screenWidth = screenRect.width();
        move(screenWidth - width(), 0);
    });
}

TimerCountWindow::~TimerCountWindow()
{
}
